﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace CIT.WMS.inventory
{
    public partial class FrmInventoryMate : Form
    {
        public FrmInventoryMate()
        {
            InitializeComponent();
            //获取盘点计划
            DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, " select InventoryName from ( select ROW_NUMBER() over (partition by InventoryName order by createtime desc) as 'idx',InventoryName,CreateTime   from MdcDatInventoryDetail where Status='0'  ) a where idx=1 order by CreateTime desc ", new CIT.WMS.MESWcf.CmdParameter[0]);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbx_inventory.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        BarcodeUtils obj = new BarcodeUtils();
        private void txt_qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 45 && e.KeyChar != 46)
            {
                if (e.KeyChar == 13)
                {

                }
                else
                {
                    txt_reelid.Text = "";
                    txt_pn.Text = "";
                    txt_qty.Text = "";
                    lab_msg.Text = "提示:只能按键盘Enter键才可以保存";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.Focus();
                    txt_reelid.SelectAll();
                }
                e.Handled = true;
            }
            //输入为负号时，只能输入一次且只能输入一次
            if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
            if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;

            if (e.KeyChar == 13)
            {
                if (txt_reelid.Text.Trim().Length == 0 || txt_qty.Text.Trim().Length == 0 || txt_pn.Text.Trim().Length == 0)
                {
                    lab_msg.Text = "提示:输入框不能为空";
                    lab_msg.ForeColor = Color.Red;
                    return;

                }
                BarcodeUtils.BarObject barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, txt_reelid.Text);
                DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, " sp_inventory_updateReel  '" + cbx_inventoryCode.Text + "','" + cbx_inventory.Text + "','" + barobj.reelId + "','" + qty + "','" + txt_qty.Text + "','" + Utils.uContext.UserNamek__BackingField + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt.Rows[0][0].ToString() == "1")
                {
                    lab_msg.Text = "提示:" + dt.Rows[0][1].ToString();
                    lab_process.Text = "盘点进度:" + dt.Rows[0][2].ToString() + "/" + dt.Rows[0][3].ToString();
                    if (dt.Rows[0][2].ToString() == dt.Rows[0][3].ToString())
                    {
                        //当前盘点计划盘点完成
                        //lab_process.Text = "盘点进度:盘点计划";
                        dt = Utils.NMS.QueryDataTable(Utils.uContext, " select InventoryName from ( select ROW_NUMBER() over (partition by InventoryName order by createtime desc) as 'idx',InventoryName,CreateTime   from MdcDatInventoryDetail where Status='0'  ) a where idx=1 order by CreateTime desc ", new CIT.WMS.MESWcf.CmdParameter[0]);
                        cbx_inventoryCode.Text = "";
                        cbx_inventory.Text = "";
                        cbx_inventory.Items.Clear();
                        cbx_inventoryCode.Items.Clear();
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            cbx_inventory.Items.Add(dt.Rows[i][0].ToString());
                        }

                    }
                    lab_msg.ForeColor = Color.Lime;
                    txt_reelid.Text = "";
                    txt_pn.Text = "";
                    txt_qty.Text = "";
                    txt_reelid.Focus();
                    return;
                }
                else
                {
                    lab_msg.Text = dt.Rows[0][1].ToString();
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.Text = "";
                    txt_qty.Text = "";
                    txt_pn.Text = "";
                    txt_reelid.Focus();
                }
            }
        }
        string qty = "";
        string INV_Code = "";
        string INV_NAME = "";
        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                BarcodeUtils.BarObject barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, txt_reelid.Text);

                lab_msg.Text = "";
                //if (cbx_inventory.Text.Length == 0)
                //{
                //    lab_msg.Text = "提示:盘点名称不可为空";
                //    lab_msg.ForeColor = Color.Red;
                //    txt_reelid.Text = "";
                //    return;
                //}
                //if (cbx_inventoryCode.Text.Length == 0)
                //{
                //    lab_msg.Text = "提示:盘点代码不可为空";
                //    lab_msg.ForeColor = Color.Red;
                //    return;
                //}
                string[] Inventory_array = cbx_inventory.Text.Split('_');

                DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, "sp_inventory_CheckReelID '" + cbx_inventoryCode.Text + "','" + cbx_inventory.Text + "','" + barobj.reelId + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0][0].ToString() == "1")
                    {
                        lab_msg.Text = dt.Rows[0][1].ToString();
                        lab_msg.ForeColor = Color.Lime;
                        //未盘点，可以正常盘点
                        txt_qty.Text = dt.Rows[0][2].ToString();
                        qty = txt_qty.Text;
                        txt_pn.Text = dt.Rows[0][3].ToString();
                        txt_qty.Focus();
                        txt_qty.SelectionStart = txt_qty.Text.Trim().Length;
                        if (dt.Rows[0][4].ToString() == "1")
                        {
                            try
                            {
                                KeySends.SendKey(13);
                            }
                            catch (Exception ee) { MessageBox.Show(ee.Message); }
                        }
                    }
                    else
                    {
                        lab_msg.Text = dt.Rows[0][1].ToString();
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.Text = "";
                        txt_qty.Text = "";
                        txt_pn.Text = "";
                        txt_reelid.Focus();
                    }
                }
            }
        }

        private void cbx_inventory_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void cbx_inventoryCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cbx_inventory_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbx_inventoryCode.Items.Clear();
            //获取盘点计划
            DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, "select distinct InventoryCode from MdcDatInventory  where Status='0' and InventoryName='" + cbx_inventory.Text + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbx_inventoryCode.Items.Add(dt.Rows[i][0].ToString());
                cbx_inventoryCode.SelectedIndex = 0;
            }
            if (cbx_inventoryCode.Items.Count > 0)
                txt_reelid.Focus();
        }

        private void cbx_inventoryCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_reelid.Text = "";
            txt_qty.Text = "";
            txt_pn.Text = "";
            txt_reelid.Focus();
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            //lab_process.Text = "盘点进度:盘点计划";
            DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, " select InventoryName from ( select ROW_NUMBER() over (partition by InventoryName order by createtime desc) as 'idx',InventoryName,CreateTime   from MdcDatInventoryDetail where Status='0'  ) a where idx=1 order by CreateTime desc ", new CIT.WMS.MESWcf.CmdParameter[0]);
            txt_reelid.Text = "";
            txt_qty.Text = "";
            txt_pn.Text = "";
            cbx_inventoryCode.Text = "";
            cbx_inventory.Text = "";
            cbx_inventory.Items.Clear();
            cbx_inventoryCode.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbx_inventory.Items.Add(dt.Rows[i][0].ToString());
            }
        }

        private void cbx_inventory_KeyUp(object sender, KeyEventArgs e)
        {
            cbx_inventory.Items.Clear();
            DataTable DT = Utils.NMS.QueryDataTable(Utils.uContext, "select top 5 InventoryName from MdcDatInventoryDetail where [status]=0 and InventoryName like '%" + cbx_inventory.Text + "%' group by InventoryName", new CIT.WMS.MESWcf.CmdParameter[0]);
            for (int i = 0; i < DT.Rows.Count; i++)
            {
                cbx_inventory.Items.Add(DT.Rows[i][0].ToString());
            }
            cbx_inventory.SelectionStart = cbx_inventory.Text.Length;
            cbx_inventory.DropDownStyle = ComboBoxStyle.DropDown;
        }
    }
}